package cn.jb698.codec.msm.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.jb698.codec.msm.generator.util.CodecDbConnectionUtils;
import cn.jb698.codec.msm.generator.v5base.beans.BcrudConfigBean;
import cn.jb698.codec.msm.generator.v5base.beans.FileOutputTpltCfg;

/**
 * Msm文件输出模板工具类
 * 
 * @author LIU(LiuJianbin)
 * @date 2021-01-29
 */
public class CodecMsmFileOutputTpltUtils {

	/**
	 * 创建JavaModel的模板
	 * 
	 * @return
	 */
	public static FileOutputTpltCfg doFileOutputTpltCfgJavaModelCreate() {
		String javaModel_tpltCode = "msm_javaModel";
		String javaModel_tpltFileRes = "/cn/jb698/codec/msm/generator/v5base/bcrud/model/JavaModel_Template_v5.vm";
		String javaModel_outputFileDir = ("/src/main/java/" + "$!{fileObjEntity.packageName}"+"/"); // CodecGeneUtils.parseAndCombineDirStr();
		String javaModel_outputFileName = "${fileObjEntity.objName}" + ".java";
		FileOutputTpltCfg javaModel_outputCfg = new FileOutputTpltCfg();
		javaModel_outputCfg.setTpltFileRes(javaModel_tpltFileRes);
		javaModel_outputCfg.setOutputFileRelDir(javaModel_outputFileDir);
		javaModel_outputCfg.setOutputFileName(javaModel_outputFileName);
		javaModel_outputCfg.setTpltCode(javaModel_tpltCode);
		return javaModel_outputCfg;
	}

	/**
	 * 旧的XML生成流程
	 * 
	 * @param configBean
	 * @param serviceFileOutputTpltCfgList
	 * @return
	 */
	public static List<FileOutputTpltCfg> doFileOutputTpltCfgAppend4Xml(BcrudConfigBean configBean, List<FileOutputTpltCfg> serviceFileOutputTpltCfgList) {

		if (serviceFileOutputTpltCfgList == null) {
			serviceFileOutputTpltCfgList = new ArrayList<>();
		}
		Map<String, FileOutputTpltCfg> curMap = serviceFileOutputTpltCfgList.stream().collect(Collectors.toMap(FileOutputTpltCfg::getTpltCode, obj -> obj, (cmp1, cmp2) -> cmp1));

		{
			FileOutputTpltCfg tmp_tpltCfg = doFileOutputTpltCfgJavaModelCreate();

			if (curMap.get(tmp_tpltCfg.getTpltCode()) == null) {
				curMap.put(tmp_tpltCfg.getTpltCode(), tmp_tpltCfg);
				serviceFileOutputTpltCfgList.add(tmp_tpltCfg);
			}
		}

		{

			// ModelMapper.xml
			String mapperXml_tpltCode = "msm_mapperXml";
			String mapperXmlOutput_tpltFileRes = "/cn/jb698/codec/msm/generator/v5base/bcrud/mapper/XmlMapper_Template_v5.vm";
			if (CodecDbConnectionUtils.DB_DIALECT_MYSQL.equals(configBean.getDbType())) {
				mapperXmlOutput_tpltFileRes = "/cn/jb698/codec/msm/generator/v5base/bcrud/mapper/XmlMapper_Template_mysql_v5.vm";
			}
			if (CodecDbConnectionUtils.DB_DIALECT_ORACLE.equals(configBean.getDbType())) {
				mapperXmlOutput_tpltFileRes = "/cn/jb698/codec/msm/generator/v5base/bcrud/mapper/XmlMapper_Template_oracle_v5.vm";
			}
			String mapperXmlOutput_outputFileDir = ("/src/main/resources/"+ configBean.getMapperXmlPackageName() +"/" ); // CodecGeneUtils.parseAndCombineDirStr("/src/main/resources/", configBean.getMapperXmlPackageName() + "/");
			String mapperXmlOutput_outputFileName = "${fileObjEntity.objName}Mapper" + ".xml";

			FileOutputTpltCfg tmp_tpltCfg = new FileOutputTpltCfg();
			tmp_tpltCfg.setTpltCode(mapperXml_tpltCode);
			tmp_tpltCfg.setTpltFileRes(mapperXmlOutput_tpltFileRes);
			tmp_tpltCfg.setOutputFileRelDir(mapperXmlOutput_outputFileDir);
			tmp_tpltCfg.setOutputFileName(mapperXmlOutput_outputFileName);

			if (curMap.get(tmp_tpltCfg.getTpltCode()) == null) {
				curMap.put(tmp_tpltCfg.getTpltCode(), tmp_tpltCfg);
				serviceFileOutputTpltCfgList.add(tmp_tpltCfg);
			}

		}
		{
			// ModelMapper.java
			String modelMapper_tpltCode = "msm_modelMapper";
			String modelMapperOutput_tpltFileRes = "/cn/jb698/codec/msm/generator/v5base/bcrud/dao/JavaMapper_Template_v5.vm";
			String modelMapperOutput_outputFileDir = ("/src/main/java/" + "$!{fileObjMapper.objFullName}");
			String modelMapperOutput_outputFileName = "${fileObjEntity.objName}Mapper" + ".java";
			FileOutputTpltCfg tmp_tpltCfg = new FileOutputTpltCfg();
			tmp_tpltCfg.setTpltCode(modelMapper_tpltCode);
			tmp_tpltCfg.setTpltFileRes(modelMapperOutput_tpltFileRes);
			tmp_tpltCfg.setOutputFileRelDir(modelMapperOutput_outputFileDir);
			tmp_tpltCfg.setOutputFileName(modelMapperOutput_outputFileName);

			if (curMap.get(tmp_tpltCfg.getTpltCode()) == null) {
				curMap.put(tmp_tpltCfg.getTpltCode(), tmp_tpltCfg);
				serviceFileOutputTpltCfgList.add(tmp_tpltCfg);
			}
		}
		{
			// ModelService.java
			String modelService_tpltCode = "msm_modelService";
			String modelServiceOutput_tpltFileRes = "/cn/jb698/codec/msm/generator/v5base/bcrud/service/JavaModelService_Template_v5.vm";
			String modelServiceOutput_outputFileDir = ("/src/main/java/" + "$!{fileObjService.objFullName}");
			String modelServiceOutput_outputFileName = "${fileObjEntity.objName}Service" + ".java";
			FileOutputTpltCfg tmp_tpltCfg = new FileOutputTpltCfg();
			tmp_tpltCfg.setTpltCode(modelService_tpltCode);
			tmp_tpltCfg.setTpltFileRes(modelServiceOutput_tpltFileRes);
			tmp_tpltCfg.setOutputFileRelDir(modelServiceOutput_outputFileDir);
			tmp_tpltCfg.setOutputFileName(modelServiceOutput_outputFileName);

			if (curMap.get(tmp_tpltCfg.getTpltCode()) == null) {
				curMap.put(tmp_tpltCfg.getTpltCode(), tmp_tpltCfg);
				serviceFileOutputTpltCfgList.add(tmp_tpltCfg);
			}
		}
		{
			// ModelServiceImpl.java
			String modelServiceImpl_tpltCode = "msm_modelServiceImpl";
			String modelServiceImplOutput_tpltFileRes = "/cn/jb698/codec/msm/generator/v5base/bcrud/service/impl/JavaModelServiceImpl_Spring_Template_v5.vm";
			String modelServiceImplOutput_outputFileDir = ("/src/main/java/" + "$!{fileObjService.packageName}" + ".impl/");
			String modelServiceImplOutput_outputFileName = "${fileObjEntity.objName}Service" + "Impl" + ".java";
			FileOutputTpltCfg tmp_tpltCfg = new FileOutputTpltCfg();
			tmp_tpltCfg.setTpltCode(modelServiceImpl_tpltCode);
			tmp_tpltCfg.setTpltFileRes(modelServiceImplOutput_tpltFileRes);
			tmp_tpltCfg.setOutputFileRelDir(modelServiceImplOutput_outputFileDir);
			tmp_tpltCfg.setOutputFileName(modelServiceImplOutput_outputFileName);

			if (curMap.get(tmp_tpltCfg.getTpltCode()) == null) {
				curMap.put(tmp_tpltCfg.getTpltCode(), tmp_tpltCfg);
				serviceFileOutputTpltCfgList.add(tmp_tpltCfg);
			}
		}
		return serviceFileOutputTpltCfgList;
	}

	/**
	 * Baomidou配套模板
	 * 
	 * @param configBean
	 * @param serviceFileOutputTpltCfgList
	 * @return
	 */
	public static List<FileOutputTpltCfg> doFileOutputTpltCfgAppend4Bmd(BcrudConfigBean configBean, List<FileOutputTpltCfg> serviceFileOutputTpltCfgList) {

		if (serviceFileOutputTpltCfgList == null) {
			serviceFileOutputTpltCfgList = new ArrayList<>();
		}
		Map<String, FileOutputTpltCfg> curMap = serviceFileOutputTpltCfgList.stream().collect(Collectors.toMap(FileOutputTpltCfg::getTpltCode, obj -> obj, (cmp1, cmp2) -> cmp1));
		{
			FileOutputTpltCfg tmp_tpltCfg = doFileOutputTpltCfgJavaModelCreate();
			if (curMap.get(tmp_tpltCfg.getTpltCode()) == null) {
				curMap.put(tmp_tpltCfg.getTpltCode(), tmp_tpltCfg);
				serviceFileOutputTpltCfgList.add(tmp_tpltCfg);
			}
		}

		{
			// ModelMapperBmd.java
			String modelMapperBmd_tpltCode = "msm_modelMapperBmd";
			String modelMapperBmdOutput_tpltFileRes = "/cn/jb698/codec/msm/generator/v5base/bcrud/daobmd/JavaMapperBmd_Template_v5.vm";
			String modelMapperBmdOutput_outputFileDir = ("/src/main/java/" + "$!{fileObjMapper.packageName}"+"/");
			String modelMapperBmdOutput_outputFileName = "${fileObjMapper.objName}" + ".java";
			FileOutputTpltCfg tmp_tpltCfg = new FileOutputTpltCfg();
			tmp_tpltCfg.setTpltCode(modelMapperBmd_tpltCode);
			tmp_tpltCfg.setTpltFileRes(modelMapperBmdOutput_tpltFileRes);
			tmp_tpltCfg.setOutputFileRelDir(modelMapperBmdOutput_outputFileDir);
			tmp_tpltCfg.setOutputFileName(modelMapperBmdOutput_outputFileName);

			if (curMap.get(tmp_tpltCfg.getTpltCode()) == null) {
				curMap.put(tmp_tpltCfg.getTpltCode(), tmp_tpltCfg);
				serviceFileOutputTpltCfgList.add(tmp_tpltCfg);
			}
		}
		{
			// ModelServiceBmd.java
			String modelServiceBmd_tpltCode = "msm_modelServiceBmd";
			String modelServiceBmdOutput_tpltFileRes = "/cn/jb698/codec/msm/generator/v5base/bcrud/servicebmd/JavaModelServiceBmd_Template_v5.vm";
			String modelServiceBmdOutput_outputFileDir = ("/src/main/java/" + "$!{basePackage}/servicebmd/");
			String modelServiceBmdOutput_outputFileName = "${fileObjEntity.objName}ServiceBmd" + ".java";
			FileOutputTpltCfg tmp_tpltCfg = new FileOutputTpltCfg();
			tmp_tpltCfg.setTpltCode(modelServiceBmd_tpltCode);
			tmp_tpltCfg.setTpltFileRes(modelServiceBmdOutput_tpltFileRes);
			tmp_tpltCfg.setOutputFileRelDir(modelServiceBmdOutput_outputFileDir);
			tmp_tpltCfg.setOutputFileName(modelServiceBmdOutput_outputFileName);

			if (curMap.get(tmp_tpltCfg.getTpltCode()) == null) {
				curMap.put(tmp_tpltCfg.getTpltCode(), tmp_tpltCfg);
				serviceFileOutputTpltCfgList.add(tmp_tpltCfg);
			}
		}
		{
			// ModelServiceBmdImpl.java
			String modelServiceBmdImpl_tpltCode = "msm_modelServiceBmdImpl";
			String modelServiceBmdImplOutput_tpltFileRes = "/cn/jb698/codec/msm/generator/v5base/bcrud/servicebmd/impl/JavaModelServiceBmdImpl_Spring_Template_v5.vm";
			String modelServiceBmdImplOutput_outputFileDir = ("/src/main/java/" + "$!{basePackage}/servicebmd/impl/");
			String modelServiceBmdImplOutput_outputFileName = "${fileObjEntity.objName}ServiceBmdImpl" + ".java";
			FileOutputTpltCfg tmp_tpltCfg = new FileOutputTpltCfg();
			tmp_tpltCfg.setTpltCode(modelServiceBmdImpl_tpltCode);
			tmp_tpltCfg.setTpltFileRes(modelServiceBmdImplOutput_tpltFileRes);
			tmp_tpltCfg.setOutputFileRelDir(modelServiceBmdImplOutput_outputFileDir);
			tmp_tpltCfg.setOutputFileName(modelServiceBmdImplOutput_outputFileName);

			if (curMap.get(tmp_tpltCfg.getTpltCode()) == null) {
				curMap.put(tmp_tpltCfg.getTpltCode(), tmp_tpltCfg);
				serviceFileOutputTpltCfgList.add(tmp_tpltCfg);
			}

		}
		return serviceFileOutputTpltCfgList;
	}

	/**
	 * Tk配套模板
	 * 
	 * @param configBean
	 * @param serviceFileOutputTpltCfgList
	 * @return
	 */
	public static List<FileOutputTpltCfg> doFileOutputTpltCfgAppend4Tk(BcrudConfigBean configBean, List<FileOutputTpltCfg> serviceFileOutputTpltCfgList) {

		if (serviceFileOutputTpltCfgList == null) {
			serviceFileOutputTpltCfgList = new ArrayList<>();
		}
		Map<String, FileOutputTpltCfg> curMap = serviceFileOutputTpltCfgList.stream().collect(Collectors.toMap(FileOutputTpltCfg::getTpltCode, obj -> obj, (cmp1, cmp2) -> cmp1));
		{
			FileOutputTpltCfg tmp_tpltCfg = doFileOutputTpltCfgJavaModelCreate();

			if (curMap.get(tmp_tpltCfg.getTpltCode()) == null) {
				curMap.put(tmp_tpltCfg.getTpltCode(), tmp_tpltCfg);
				serviceFileOutputTpltCfgList.add(tmp_tpltCfg);
			}
		}

		{
			// ModelMapperTk.java
			String modelMapperTk_tpltCode = "msm_modelMapperTk";
			String modelMapperTkOutput_tpltFileRes = "/cn/jb698/codec/msm/generator/v5base/bcrud/daotk/JavaMapperTk_Template_v5.vm";
			String modelMapperTkOutput_outputFileDir = ("/src/main/java/" + "$!{fileObjMapper.packageName}tk"+"/");
			String modelMapperTkOutput_outputFileName = "${fileObjEntity.objName}MapperTk" + ".java";
			FileOutputTpltCfg tmp_tpltCfg = new FileOutputTpltCfg();
			tmp_tpltCfg.setTpltCode(modelMapperTk_tpltCode);
			tmp_tpltCfg.setTpltFileRes(modelMapperTkOutput_tpltFileRes);
			tmp_tpltCfg.setOutputFileRelDir(modelMapperTkOutput_outputFileDir);
			tmp_tpltCfg.setOutputFileName(modelMapperTkOutput_outputFileName);
			if (curMap.get(tmp_tpltCfg.getTpltCode()) == null) {
				curMap.put(tmp_tpltCfg.getTpltCode(), tmp_tpltCfg);
				serviceFileOutputTpltCfgList.add(tmp_tpltCfg);
			}
		}
		return serviceFileOutputTpltCfgList;
	}

//	/**
//	 * 
//	 * @param configBean
//	 * @param tableBcrudBean
//	 * @param serviceFileOutputTpltCfgList
//	 * @return
//	 */
//	private static List<FileOutputTpltCfg> doAddMsmServBcrudV5OutputTpltCfgToList(BcrudConfigBean configBean, BcrudCfgTableBean tableBcrudBeanx, List<FileOutputTpltCfg> serviceFileOutputTpltCfgList) {
//
//		if (serviceFileOutputTpltCfgList == null) {
//			serviceFileOutputTpltCfgList = new ArrayList<>();
//		}
//
//		Boolean hasPersistence = tableBcrudBeanx.getHasPersistence();
//
//		if (hasPersistence != null && hasPersistence) {
//
//		}
//		Boolean hasBaomidou = tableBcrudBeanx.getHasBaomidou();
//		if (hasBaomidou != null && hasBaomidou) {
//
//		}
//		return serviceFileOutputTpltCfgList;
//	}

}
